﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
    public class ReViewDAL
    {
        //增
        public static bool addDAL(ReView reView)
        {
            string sql = string.Format("insert into tb_ReView (UserID,GoodsID,ReviewContent,Manyi) values ({0},{1},'{2}',{3})", reView.UserID, reView.GoodsID, reView.ReviewContent, reView.Manyi);
            return DBHelper.ExecuteNonQuery(sql);
        }
        //查根据GoodsID
        public static List<ReView> getAllDAL(int GoodsID)
        {
            string sql=string.Format("select * from tb_ReView where GoodsID={0}",GoodsID);
            List<ReView> list = new List<ReView>();
            SqlDataReader dr=DBHelper.GetDataReader(sql);
            while (dr.Read())
            {
                ReView reView = new ReView();
                reView.ReviewID = dr.GetInt32(0);
                reView.UserID= dr.GetInt32(1);
                reView.GoodsID = dr.GetInt32(2);
                reView.ReviewContent = dr.GetString(3);
                reView.ReviewTime=dr.GetDateTime(4).ToString("yyyy/MM/dd");
                reView.Manyi = dr.GetInt32(5);
                list.Add(reView);
            }
            dr.Close();
            return list;
        }
        //联表查询 ReView_userInfo
        public static List<ReView_UserInfo> getUser_ReDAL(int GoodsID)
        {
            string sql = string.Format("select r.ReviewID,r.UserID,r.GoodsID,r.ReviewContent,r.ReviewTime,r.Manyi,u.UserName from tb_ReView r,tb_UserInfo u where r.UserID=u.UserID and r.GoodsID={0}", GoodsID);
            List<ReView_UserInfo> list= new List<ReView_UserInfo>();
            SqlDataReader dr= DBHelper.GetDataReader(sql);
            while (dr.Read())
            {
                ReView_UserInfo reView = new ReView_UserInfo();
                reView.ReviewID = dr.GetInt32(0);
                reView.UserID = dr.GetInt32(1);
                reView.GoodsID = dr.GetInt32(2);
                reView.ReviewContent = dr.GetString(3);
                reView.ReviewTime = dr.GetDateTime(4).ToString("yyyy/MM/dd");
                reView.Manyi = dr.GetInt32(5);
                reView.UserName=dr.GetString(6);
                list.Add(reView);
            }
            dr.Close();
            return list;
        }
    }
}
